<template>
  <basic-dialog
    width="700px"
    title="选择品牌"
    :visible="visible"
    :showFooter="false"
    :handleClose="handleCloseDialog"
    :handleOpen="handleOpen"
  >
    <BasicForm
      :colNumOfRow="2"
      :isScalingBtn="false"
      :hasQueryPermission="false"
      :formColumns="formItemList"
      :dataSource="formData"
      :handleSearchForm="handleQueryForm"
      @getFormData="getFormData"
    />
    <BasicTable
      showActionCol
      :showSelectCol="false"
      :loading="tableInfo.loading"
      :columns="tableInfo.columns"
      :dataSource="tableInfo.list"
      :pagination="pagination"
      :pageSizes="pageSizes"
      :total="total"
      :tableHeight="450"
      :actionRender="renderTableBtnAction"
      @pageChange="handlePageChange"
      @pageSizeSelect="handlePageSizeChange"
    />
  </basic-dialog>
</template>
<script>
import BasicDialog from '@/components/BasicDialog';
import BasicForm from '@/components/BasicForm';
import BasicTable from '../BasicList';
import basePage from '@/mixins/commodity/skusPage';
import { getTableAction } from '@/utils/getBtnsRender';
import { queryBrandService } from '@/api/sourcing/public';

export default {
  mixins: [basePage],
  components: {
    BasicDialog,
    BasicForm,
    BasicTable
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    categoryNo: {
      type: [String, Number]
    }
  },
  data () {
    return {
      formData: { condition: '' }, // 品牌名称
      formItemList: [{ label: '品牌名称', prop: 'condition', type: 'input' }],
      tableInfo: {
        list: [],
        columns: [
          { label: '品牌ID',  prop: 'uuid' },
          { label: '品牌名称',  prop: 'brandName' },
        ],
        loading: false
      },
      pagination: {
        pageNum: 1, // 当前页码
        pageSize: 50 // 当前页码size
      },
    }
  },
  methods: {
    // 渲染表格按钮
    renderTableBtnAction (h, { row }) {
      const actionList = [{
        label: '选择',
        method: this.handleChooseBrand
      }];
      return getTableAction({ h, row, actionList })
    },
    // 获取表单查询入参
    getSearchFormParams () {
      return {
        ...this.pagination,
        condition: this.formData.condition,
        categoryNo: this.categoryNo
      }
    },
    // 获取表格数据
    async getTableData () {
      const res = await queryBrandService(this.getSearchFormParams(), state => this.tableInfo.loading = state)
      if (res?.data) {
        this.tableInfo.list = res.data?.results ?? [];
        this.total = res.data?.totalNum ?? 0;
      }      
    },
    // 选择品牌
    handleChooseBrand (row) {
      const { brandName, uuid } = row;
      this.$emit('getBrandInfo', { brandName, uuid });
      this.handleCloseDialog();
    },
    // 关闭弹框
    handleCloseDialog () {
      this.$emit('update:visible', false);
    },
    handleOpen () {
      this.formData = { 
        condition: ''
      }
      this.tableInfo.list = []
      this.total = 0
    }
  }
}
</script>
<style lang="less" scoped>
/deep/ .el-dialog__header {
  border-bottom: 1px solid #e8eaec;
}
/* 分页样式 */
.pagination_wrap {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>
